/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UserInterface;

import bluetooth.PcClient;
import com.intel.bluetooth.BlueCoveImpl;
import java.awt.CardLayout;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.bluetooth.RemoteDevice;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import javax.microedition.io.StreamConnection;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Aida
 */
public class ConnectPanel extends javax.swing.JPanel {

    JPanel userProcessContainer; 
    Vector btDevices;
    PcClient btConnect;
    int index = 0;
    
    //*********************************Wocket Commands***************************************************************
    public final static byte[] WOCKET_60_SEC_BURST_PACKET = {(byte) 0xBA, (byte)0x20};
    public final static byte[] WOCKET_Continuous_PACKET = {(byte) 0xBA, (byte)0x00};
    public final static byte[] WOCKET_GET_WTM_PACKET = {(byte) 0xBE};
    public final static byte[] WOCKET_GET_BATTERY_LEVEL = {(byte) 0xA0};
    public final static byte[] WOCKET_BATTERY_CALIBRATION_PACKET = {(byte) 0xB5, 0,0,0,0,0,0,0,0,0};
    public final static byte[] WOCKET_SET_LED_PACKET = {(byte) 0xBC, (byte)0x02}; //Yellow_ LED on for 4 Seconds    
    public final static byte[] WOCKET_SET_TCT_PACKET = {(byte) 0xB9, (byte)0x1E, (byte) 0x80, (byte)0x1F, (byte)0x70};
    //****************************************************************************************************************
    
    public ConnectPanel(JPanel upc, Vector btDevices) {
        initComponents();        
        
        this.btDevices = btDevices;
        userProcessContainer = upc;
        refresh();  
        PcClient.beepRunnable.run();
        wocketsTable.setRowSelectionAllowed(true);
        wocketsTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        
        
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jScrollPane1 = new javax.swing.JScrollPane();
        wocketsTable = new javax.swing.JTable();
        redAButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        refreshButton = new javax.swing.JButton();
        messageLabel = new javax.swing.JLabel();
        redWButton = new javax.swing.JButton();
        greenWButton = new javax.swing.JButton();
        greenAButton = new javax.swing.JButton();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        wocketsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Index", "Wocket Name", "Wocket Address"
            }
        ));
        jScrollPane1.setViewportView(wocketsTable);

        redAButton.setText("Match to Red A");
        redAButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                redAButtonActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Select The Red Wockets with lable \"A\" from the above table and press OK:");

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("PC-Wocket Application");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Connecting to Wocket");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setText("Available Wockets:");

        refreshButton.setText("Refresh");
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });

        messageLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        messageLabel.setText("   ");

        redWButton.setText("Match to Red W");

        greenWButton.setText("Match to Green W");

        greenAButton.setText("Match to Green A");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(greenAButton)
                                .addComponent(redWButton)
                                .addComponent(redAButton))
                            .addComponent(greenWButton))
                        .addGap(127, 127, 127))
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(251, 251, 251)
                                .addComponent(refreshButton))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 442, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(386, 386, 386)
                                .addComponent(messageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 323, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(163, 163, 163)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jLabel3)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(refreshButton)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addComponent(messageLabel))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(redAButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(redWButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(greenAButton)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(greenWButton)
                .addContainerGap(38, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    private void refresh(){
        int rowCount = wocketsTable.getRowCount();
        int i;

        for ( i= rowCount-1; i>= 0; i--){
        ((DefaultTableModel)wocketsTable.getModel()).removeRow(i);   
        }

        ArrayList <String> itemList = new ArrayList <String>();
        int btSize = btDevices.size();
        int cnt = 0;
        for (int k=0; k<btSize; k++){
            RemoteDevice remoteDevice=(RemoteDevice)btDevices.elementAt(k);
            String adr= remoteDevice.getBluetoothAddress();            
            if (adr.contains("0006660")){
                cnt++;
                Object[]new_row = new Object[3];
                new_row[0] = cnt;
                new_row[1] = "Wocket-"+adr.substring(8);
                new_row[2] = remoteDevice.getBluetoothAddress();
                ((DefaultTableModel)wocketsTable.getModel()).addRow(new_row);            
            }else{
                btDevices.removeElementAt(k);
                k--;
                btSize--;
            }
        }
    }
    
    private void redAButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_redAButtonActionPerformed
        StreamConnection streamConnection = null; 
        OutputStream outStream = null;
        InputStream inStream = null;
        
        int row = wocketsTable.getSelectedRow();
        if (row == -1){
            JOptionPane.showMessageDialog(null, "Please select the Wocket(s) you want to calibrate...");
            return;
        }
        redAButton.setVisible(false);
        refreshButton.setVisible(false);
        messageLabel.setText("Select The Red Wockets with lable \"W\" from the above table and press OK:");
        int[] indeces = wocketsTable.getSelectedRows();
        
        
        
        int num = indeces.length;
        RemoteDevice[] remoteDevice =  new RemoteDevice[num]; 
        for (int j=0; j <num; j++){            
            remoteDevice[j] = (RemoteDevice)btDevices.elementAt(indeces[j]);
        }
        /*try {
            PcClient.collectData(remoteDevice);
        } catch (IOException ex) {
            Logger.getLogger(ConnectPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(ConnectPanel.class.getName()).log(Level.SEVERE, null, ex);
        }*/
        
        
    }//GEN-LAST:event_redAButtonActionPerformed

    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed
        try {
            PcClient.findDevices();
        } catch (IOException ex) {
            Logger.getLogger(ConnectPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        btDevices = PcClient.vecDevices;
        refresh();
    }//GEN-LAST:event_refreshButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton greenAButton;
    private javax.swing.JButton greenWButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel messageLabel;
    private javax.swing.JButton redAButton;
    private javax.swing.JButton redWButton;
    private javax.swing.JButton refreshButton;
    private javax.swing.JTable wocketsTable;
    // End of variables declaration//GEN-END:variables
}
